//
//  XJ_LoginViewController.m
//  XunJieProject
//
//  Created by 薛子 on 2018/6/7.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_LoginViewController.h"
#import "AppDelegate.h"
#import "XJ_ForgetPasswordViewController.h"
#import "XJ_RegiertUserdViewController.h"
#import "TregularexpressionNSObjec.h"
@interface XJ_LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *loginTxt;

@property (weak, nonatomic) IBOutlet UITextField *passWordTxt;
@property (weak, nonatomic) IBOutlet UIView *passWordView;

@property (weak, nonatomic) IBOutlet UIView *loginView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation XJ_LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.nav_btmView.backgroundColor = [UIColor clearColor];
    self.nav_titleLabel.text = @"登录";
    self.nav_titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.nav_backButton.hidden = YES;
//    [self.nav_backButton setImage:[UIImage imageNamed:@"close_btn"] forState:UIControlStateNormal];
//    self.nav_backButton.frame = CGRectMake(20, 30, 20, 20);
    
    _loginTxt.keyboardType = UIKeyboardTypeNumberPad;
    _loginView.layer.cornerRadius = 4;
    _loginView.layer.borderWidth = 1.5;
    _loginView.layer.borderColor  = [UIColor whiteColor].CGColor;
    [_loginTxt addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    _passWordView.layer.cornerRadius = 4;
    _passWordView.layer.borderWidth = 1.5;
    _passWordView.layer.borderColor  = [UIColor whiteColor].CGColor;
    
    
    _loginBtn.layer.cornerRadius = 4;
    

    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:
                                      @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                        NSFontAttributeName:_loginTxt.font
                                        }];
    _loginTxt.attributedPlaceholder = attrString;
    
    NSAttributedString *attrString2 = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:
                                      @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                        NSFontAttributeName:_passWordTxt.font
                                        }];
    _passWordTxt.attributedPlaceholder = attrString2;

    // Do any additional setup after loading the view from its nib.
}

- (IBAction)registerUserd:(id)sender {
    
    XJ_RegiertUserdViewController * resiertVC = [[XJ_RegiertUserdViewController alloc]init];
    [self.navigationController pushViewController:resiertVC animated:YES];
}

- (IBAction)missPassWord:(id)sender {
    
    XJ_ForgetPasswordViewController * forgetVC = [[XJ_ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}


- (IBAction)login:(id)sender {

    if (_loginTxt.text.length == 0) {
        
        [XJ_Utils alertViewWithMessage:@"请输入账号"];
        return;
    }
    
    if (_passWordTxt.text.length == 0) {
        
        [XJ_Utils alertViewWithMessage:@"请输入密码"];
        return;
    }
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:_loginTxt.text forKey:@"mobile"];
//    [dic setValue:[[_passWordTxt.text md5HexDigest] uppercaseString] forKey:@"password"];
    [dic setValue:_passWordTxt.text forKey:@"password"];
    [dic setValue:@"123" forKey:@"registrationId"];
    [XJ_Utils post:@"user/login?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        NSString * tokenStr = [dataDic objectForKey:@"token"];
        NSString * userid = [dataDic objectForKey:@"userId"];
        
        NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
        [db setObject:[tokenStr lowercaseString] forKey:@"token"];
        [db setObject:userid forKey:@"userId"];
        [db synchronize];
        
        /**进入首页**/
        [(AppDelegate*)[UIApplication sharedApplication].delegate crearTabBarController];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
   
}
- (void)textFieldDidChange:(UITextField *)textField

{
    
    if (textField == self.loginTxt) {
        
        if (textField.text.length > 11) {
            
            textField.text = [textField.text substringToIndex:11];
            
        }
        
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
